#include "quesion7.h"
using namespace std;
#include "stdlib.h"
#include <string>
#include <iostream>
#include <climits>

int quesion7::reverse(int x)
{
/*  1
    if(x>0){
        if(x%10!=0){
            std::string pos1=std::to_string(x);
            std::string rpos1;
            for(int i=0;i<pos1.size();i++){
                rpos1[i]=pos1[pos1.size()-1-i];
            }
            return atoi(rpos1.c_str());
        }
        if(x%10==0){
            std::string pos2=std::to_string(x);
            std::string rpos2;
            for(int i=0;i<pos2.size();i++){
                if(pos2[pos2.size()-1-i]!=0){
                    rpos2[i]=pos2[pos2.size()-1-i];
                }
            }
            return atoi(rpos2.c_str());
        }
    }
    else if(x<0){
        int y=abs(x);
        if(y%10!=0){
            std::string neg1=std::to_string(y);
            std::string rneg1;
            for(int i=0;i<neg1.size();i++){
                rneg1[i]=neg1[neg1.size()-1-i];
            }
            int z=atoi(rneg1.c_str());
            z=-z;
            return z;
        }
        if(y%10==0){
            std::string neg2=std::to_string(y);
            std::string rneg2;
            for(int i=0;i<neg2.size();i++){
                if(neg2[neg2.size()-1-i]!=0){
                    rneg2[i]=neg2[neg2.size()-1-i];
                }
            }
            int u=atoi(rneg2.c_str());
            u=-u;
            return u;
        }
    }
    else
    {
        return 0;
    }
*/
/* 2
    if(x>0){
            if(x%10!=0){
                std::string pos1=std::to_string(x);
                std::string rpos1;
                for(int i=0;i<pos1.size();i++){
                    rpos1[i]=pos1[pos1.size()-1-i];
                }
                return atoi(rpos1.c_str());
            }
            else{
                std::string pos2=std::to_string(x);
                std::string rpos2;
                for(int i=0;i<pos2.size();i++){
                    if(pos2[pos2.size()-1-i]!=0){
                        rpos2[i]=pos2[pos2.size()-1-i];
                    }
                }
                return atoi(rpos2.c_str());
            }
        }
            else if(x<0){
                int y=abs(x);
                if(y%10!=0){
                    std::string neg1=std::to_string(y);
                    std::string rneg1;
                    for(int i=0;i<neg1.size();i++){
                        rneg1[i]=neg1[neg1.size()-1-i];
                    }
                    int z=atoi(rneg1.c_str());
                    z=-z;
                    return z;
                }
                else{
                    std::string neg2=std::to_string(y);
                    std::string rneg2;
                    for(int i=0;i<neg2.size();i++){
                        if(neg2[neg2.size()-1-i]!=0){
                            rneg2[i]=neg2[neg2.size()-1-i];
                        }
                    }
                    int u=atoi(rneg2.c_str());
                    u=-u;
                    return u;
                }
            }
            else
            {
                return 0;
            }
*/
/* 3
    if(x>0){
                if(x%10!=0){
                    std::string pos1=std::to_string(x);
                    char rpos1[pos1.size()];
                    for(int i=0;i<pos1.size();i++){
                        rpos1[i]=pos1[pos1.size()-1-i];
                    }
                    return atoi(rpos1);
                }
                else{
                    std::string pos2=std::to_string(x);
                    char rpos2[pos2.size()];
                    for(int i=0;i<pos2.size();i++){
                        if(pos2[pos2.size()-1-i]!=0){
                            rpos2[i]=pos2[pos2.size()-1-i];
                        }
                    }
                    return atoi(rpos2);
                }
            }
                else if(x<0){
                    int y=abs(x);
                    if(y%10!=0){
                        std::string neg1=std::to_string(y);
                        char rneg1[neg1.size()];
                        for(int i=0;i<neg1.size();i++){
                            rneg1[i]=neg1[neg1.size()-1-i];
                        }
                        int z=atoi(rneg1);
                        z=-z;
                        return z;
                    }
                    else{
                        std::string neg2=std::to_string(y);
                        char rneg2[neg2.size()];
                        for(int i=0;i<neg2.size();i++){
                            if(neg2[neg2.size()-1-i]!=0){
                                rneg2[i]=neg2[neg2.size()-1-i];
                            }
                        }
                        int u=atoi(rneg2);
                        u=-u;
                        return u;
                    }
                }
                else
                {
                    return 0;
                }
*/
    if(x>0){
        if(x%10!=0){
            std::string pos1=std::to_string(x);
            std::string rpos1;
            rpos1.resize(pos1.size());
            for(int i=0;i<pos1.size();i++){
                rpos1[i]=pos1[pos1.size()-1-i];
            }
            long int u=atol(rpos1.c_str());
            return u <= INT_MIN || u >= INT_MAX ? 0:u;
        }
        else{
            std::string pos2=std::to_string(x);
            std::string rpos2;
            rpos2.resize(pos2.size());
            for(int i=0;i<pos2.size();i++){
                if(pos2[pos2.size()-1-i]!=0){
                    rpos2[i]=pos2[pos2.size()-1-i];
                }
            }
            long int u=atol(rpos2.c_str());
            return u <= INT_MIN || u >= INT_MAX ? 0:u;
        }
        }
    else if(x<0){
        long int y=abs(x);
        if(y%10!=0){
            std::string neg1=std::to_string(y);
            std::string rneg1;
            rneg1.resize(neg1.size());
            for(int i=0;i<neg1.size();i++){
                rneg1[i]=neg1[neg1.size()-1-i];
            }
            long int z=atol(rneg1.c_str());
            z=-z;
            return z <= INT_MIN || z >= INT_MAX ? 0:z;
        }
        else{
            std::string neg2=std::to_string(y);
            std::string rneg2;
            rneg2.resize(neg2.size());
            for(int i=0;i<neg2.size();i++){
                if(neg2[neg2.size()-1-i]!=0){
                    rneg2[i]=neg2[neg2.size()-1-i];
                }
            }
            long int u=atol(rneg2.c_str());
            u=-u;
            return u <= INT_MIN || u >= INT_MAX ? 0:u;
        }
    }
    else
    {
        return 0;
    }
}


